<?php 
if(!isset($_SESSION["id_membre"])) {
session_start();
}


require("../../lib/mysql_connect.php");

function get_eleve()
{
  $requete_eleve = "SELECT id_eleve, nom, prenom FROM eleve WHERE
    id_enseignant = '".$_SESSION['id_membre']."' ";

  $requete_eleve_result = mysql_query($requete_eleve);

  if( $requete_eleve_result != FALSE )
  {
    $nb_eleve = mysql_num_rows($requete_eleve_result);
    if( $nb_eleve )
    {
      $result = array();
      while($row = mysql_fetch_array($requete_eleve_result)) 
        array_push( $result, $row['id_eleve'] );

      return $result;
    }
    else
      return array();
  }
  else
    return array();
}

$eleve_liste = get_eleve();
$success = TRUE;

for($i = 0; $i<count($eleve_liste); $i++)
{
  $success = $success && mysql_query("START TRANSACTION");

  $delete_identifiant_req = "DELETE FROM identifiant_eleve WHERE id_eleve = '".$eleve_liste[$i]."' ";
  $delete_identifiant_result = mysql_query($delete_identifiant_req);
  $success = $success && $delete_identifiant_result;

  $delete_projet_req = "DELETE FROM projet WHERE id_eleve = '".$eleve_liste[$i]."' ";
  $delete_projet_result = mysql_query($delete_projet_req);
  $success = $success && $delete_projet_result;

  $delete_bilan_seance_req = "DELETE FROM bilan_seance WHERE bilan_seance.id_seance IN
    (SELECT seance.id_seance FROM seance WHERE id_eleve = '".$eleve_liste[$i]."') ";
  $delete_bilan_seance_result = mysql_query($delete_bilan_seance_req);
  $success = $success && $delete_bilan_seance_result;

  $delete_exercice_projet_req = "DELETE FROM exercice_projet_seance WHERE id_atelier_projet_seance IN
    (SELECT id_atelier_projet_seance FROM atelier_projet_seance WHERE id_seance IN
    (SELECT id_seance FROM seance WHERE id_eleve = '".$eleve_liste[$i]."')
  ) ";   
  $delete_exercice_projet_result = mysql_query($delete_exercice_projet_req);
  $success = $success && $delete_exercice_projet_result;

  $delete_atelier_projet_req = "DELETE FROM atelier_projet_seance WHERE id_seance IN
    (SELECT id_seance FROM seance WHERE id_eleve='".$eleve_liste[$i]."') ";
  $delete_atelier_projet_result = mysql_query($delete_atelier_projet_req);
  $success = $success && $delete_atelier_projet_result;

  $delete_exercice_bilan_req = "DELETE FROM exercice_bilan_seance WHERE id_atelier_bilan_seance IN
    (SELECT id_atelier_bilan_seance FROM atelier_bilan_seance WHERE id_seance IN
    (SELECT id_seance FROM seance WHERE id_eleve = '".$eleve_liste[$i]."')
  ) ";   
  $delete_exercice_bilan_result = mysql_query($delete_exercice_bilan_req);
  $success = $success && $delete_exercice_bilan_result;

  $delete_atelier_bilan_req = "DELETE FROM atelier_bilan_seance WHERE id_seance IN
    (SELECT id_seance FROM seance WHERE id_eleve='".$eleve_liste[$i]."') ";
  $delete_atelier_bilan_result = mysql_query($delete_atelier_bilan_req);
  $success = $success && $delete_atelier_bilan_result;

  $delete_seance_req = "DELETE FROM seance WHERE id_eleve='".$eleve_liste[$i]."' ";
  $delete_seance_result = mysql_query($delete_seance_req);
  $success = $success && $delete_seance_result;

  $delete_eleve_req = "DELETE FROM eleve WHERE id_eleve='".$eleve_liste[$i]."' ";
  $delete_eleve_result = mysql_query($delete_eleve_req);
  $success = $success && $delete_eleve_result;

  $success = $success && mysql_query("COMMIT");

}

if( $success )
  echo "<div class='success_div'>Toutes les informations du cycle passé ont bien été effacées</div>";
else
  echo "<div class='success_div'>Une erreur est survenue lors de la mise à jour de la base de données</div>";

?>
